from strategy_mode import dataLoader, str_cal, Trading, TradingRules

# 上涨模式
def UpMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):
    # elif TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="UP") >= 3:
    statsdic["TYPES"] = "HIGH"
    # 超级信号
    if (
        statsdic["Share"] > 0
        and (
            TradingRules.Main_compare_or(df_day, count_in, num=1.6, mode="UP")
            or TradingRules.Main_Plus(df_day, count_in, num=0.3)
        )
        and (
            TradingRules.MA_Key_key_Boll(
                df_day,
                numL=[TradingRules.MA_LastNoneZero(df_day, day_lists)[0]],
                boll=1,
            )
        )
    ):

        mode = "SELL"
        tradePersent = 1

    # 变盘
    # elif (
    #     # TradingRules.MA_Key_key_Boll(
    #     #     df,
    #     #     day,
    #     #     numL=[TradingRules.MA_LastNoneZero(df, day_lists, day)[0]],
    #     #     boll=-1,
    #     # ) or
    #     (
    #         (df.iloc[day - 1 ]["close_by%_sum"] <= 0 and df_day["close_by%_sum"] >= 0)
    #         # TradingRules.MA_Boll_by(
    #         #     df,
    #         #     day,
    #         #     numL=[TradingRules.MA_LastNoneZero(df, day_lists, day)[0]],
    #         #     boll=1,
    #         # )
    #     )
    #     # and preTYPES == "ROUND"
    # ):
    #     # print(TradingRules.MA_LastNoneZero(df, day_lists, day))
    #     mode = "BUY"
    #     if TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=1):
    #         tradePersent = 0.5

    # # 普通信号
    # elif (
    #     statsdic["Share"] > 0
    #     and (
    #         df_day.open
    #         <= df_day.close_ma_3
    #         #    and df_day.open >= df_day.close
    #     )
    #     and TradingRules.MA_Boll_by(df, day, numL=[13, 21], boll=1)
    # ):
    #     mode = "SELL"
    #     tradePersent = 1
    #     if TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="UP") >= 6:
    #         tradePersent = 0.3
    #     # elif Ma_Compare(df, day_lists, day, key='close'):
    #     #     tradePersent = 0.7

    # 追涨
    # elif (
    #     (TradingRules.MA_Key_key_Boll(df, day, numL=[13], boll=1))
    #     and TradingRules.MA_Boll_by(df, day, numL=[13, 21])
    #     # and TradingRules.Sum_inc(df, day) == ("G" or "E")
    #     # and TradingRules.Main_compare_Both(df, day, count_in, num=1.5, mode="DOWN")
    # ):
    #     mode = "BUY"
    #     # tradePersent = 0.05
    #     # if TradingRules.Sum_cross(df, day) == "G" and TradingRules.MA_Key_key_Boll(
    #     #     df, day, numL=[3], boll=-1
    #     # ):
    #     tradePersent = 1
    return mode, tradePersent


# 下跌模式
def DownMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):
    # if TradingRules.MA_Sorter(df, day_lists, day, method="sort", mode="DOWN") <= -3:

    # if (
    #     # TradingRules.Sum_inc(df, day) == ("L" or "E")
    #     # and
    #     (
    #         TradingRules.MA_Boll_by(df, day, numL=[3, 5], boll=-1)
    #         or TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1)
    #     )
    #     and statsdic["Share"] > 0
    # ):

    #     mode = "SELL"
    #     tradePersent = 1
    # else:
    #     if (
    #         # TradingRules.MA_Key_key_Boll(
    #         #     df,
    #         #     day,
    #         #     numL=[TradingRules.MA_LastNoneZero(df, day_lists, day)[0]],
    #         #     boll=-1,
    #         # )
    #         # and
    #         TradingRules.Main_compare_Both(df, day, -count_in, num=4, mode="UP")
    #         and TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=1)
    #     ):
    #         mode = "BUY"
    #         tradePersent = 0.5
    #         if (
    #             TradingRules.Sum_Rolling(df, day) == "G"
    #             or TradingRules.Sum_cross(df, day) == "G"
    #         ):
    #             tradePersent = 1
    #     else:
    #         tradePersent = 0.2

    return mode, tradePersent


def RoundMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):
    # else:  # 横盘模式
    # if TradingRules.MA_Boll_by(df, day, numL=[21, 55], boll=1):  # 上升趋势
    if statsdic["Share"] > 0 and (
        TradingRules.Main_Plus(df_day, count_in, num=2.5)
        or TradingRules.Main_compare_or(df_day, count_in, num=2, mode="UP")
    ):
        tradePersent = 1
        mode = "SELL"
        # if :  # 超级信号
        # elif TradingRules.MA_Key_key_Boll(df, day, numL=[21, 55], boll=1):
        #     tradePersent = 0.8
        # elif TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=1):
        #     tradePersent = 0.5
        #     if TradingRules.MA_Key_key_Boll(
        #         df, day, numL=[3], boll=1
        #     ) and TradingRules.MA_Key_key_Boll(
        #         df, day, key="low", numL=[3], boll=1
        #     ):
        #         tradePersent = 1
    else:  # 下降趋势
        if df_day_1["close_by%_sum"] <= 0 and df_day["close_by%_sum"] >= 0:
            mode = "buy"
            tradePersent = 1

    #     if (
    #         statsdic["Share"] > 0
    #         and TradingRules.Sum_Rolling(df, day) == "X"
    #         and TradingRules.MA_Boll_by(df, day, numL=[3, 5], boll=-1)
    #         and TradingRules.MA_Boll_by(df, day, numL=[13, 21], boll=-1)
    #         and (
    #             df_day.open
    #             <= df_day.close_ma_3
    #             #  and df_day.open >= df_day.close
    #         )
    #     ):
    #         mode = "SELL"
    #         if TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1):
    #             tradePersent = 1
    #         elif TradingRules.MA_Key_key_Boll(df, day, numL=[3], boll=1):
    #             tradePersent = 0.5

    #     elif (
    #         TradingRules.MA_Boll_by(df, day, numL=[21, 55, 89], boll=1)
    #         and TradingRules.Sum_inc(df, day) == ("G" or "E")
    #         # and MA_Key_key_Boll(df, day, numL=[MA_LastNoneZero(df, day_lists, day)[0]],  boll=1)
    #     ):
    #         mode = "BUY"
    #         tradePersent = 0.3
    #         if (
    #             TradingRules.Main_Plus(df, day, count_in, num=0.5, mode="DOWN")
    #             and TradingRules.Sum_Rolling(df, day) == "G"
    #             # and TradingRules.MA_Key_key_Boll(
    #             #     df, day, numL=[3, 5, 13], boll=1
    #             # )
    #         ):
    #             tradePersent = 1
    #         elif TradingRules.Sum_Rolling(df, day) == "G" and TradingRules.MA_Boll_by(
    #             df, day, numL=[3, 5], boll=-1
    #         ):
    #             tradePersent = 0.8
    #         elif TradingRules.Main_compare_or(df, day, count_in, num=0.5, mode="DOWN"):
    #             tradePersent = 0.5

    #         elif TradingRules.Sum_cross(
    #             df, day
    #         ) == "G" and TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1):
    #             tradePersent = 0.7
    # elif TradingRules.MA_Key_key_Boll(df, day, numL=[3, 5], boll=-1):
    #     mode = "BUY"
    #     if TradingRules.MA_Boll_by(df, day, numL=[13, 21], boll=1):
    #         tradePersent = 0.5
    #     elif TradingRules.MA_Key_key_Boll(
    #         df, day, numL=[13, 21], boll=-1
    #     ) and TradingRules.MA_Boll_by(df, day, numL=[55, 89], boll=1):
    #         tradePersent = 0.7

    return mode, tradePersent


def Main(
    df_d,
    df_day,
    df_day_1,
    mode="BUY/SELL",
    line_Pares=5,
    tradePersent=1,
    day_lists=[],
    day=0,
    statsdic={},
    count_in=1,
    trade_cuts=1,
):

    preTYPES = statsdic["TYPES"]

    # 下跌模式

    if df_day["close_sort"] <= -0.3:
        statsdic["TYPES"] = "LOW"
        if statsdic["Share"] > 0 and (
            round(df_day["close_by%Boll_sum"], 2)
            == round(df_day["open_by%Boll_sum"], 2)
            == round(df_day["low_by%Boll_sum"], 2)
            == round(df_day["high_by%Boll_sum"], 2)
            and round(df_day["close_by%Boll_sum"], 2) >= 0.3
            # and TradingRules.Main_compare_or(df_day, 1, num=0.5, mode="UP")
            # TradingRules.Main_Plus(df, day, count_in, num=2) or
        ):
            tradePersent = 1
            mode = "SELL"
        elif (
            round(df_day["close_by%Boll_sum"], 2)
            == round(df_day["open_by%Boll_sum"], 2)
            == round(df_day["low_by%Boll_sum"], 2)
            == round(df_day["high_by%Boll_sum"], 2)
            and round(df_day["close_by%Boll_sum"], 2) <= -0.9
            and TradingRules.Main_compare_or(df_day, -1, num=0.5, mode="DOWN")
            #     df_day_1["close_by%Boll_sum"] >= 0 and df_day["close_by%Boll_sum"] < 0) or (
            #     df_day_1["close_by%Boll_sum"] == 0 and df_day["close_by%Boll_sum"] == 0
        ):
            mode = "BUY"
            tradePersent = 0.2

    # # 上涨模式
    elif df_day["close_sort"] <= 0.3:
        statsdic["TYPES"] = "HIGH"
        if statsdic["Share"] > 0 and (
            round(df_day["close_by%Boll_sum"], 2)
            == round(df_day["open_by%Boll_sum"], 2)
            == round(df_day["low_by%Boll_sum"], 2)
            == round(df_day["high_by%Boll_sum"], 2)
            and round(df_day["close_by%Boll_sum"], 2) >= 0.6
            and TradingRules.Main_compare_or(df_day, 1, num=0.5, mode="UP")
            # TradingRules.Main_Plus(df, day, count_in, num=2) or
        ):
            tradePersent = 0.2
            mode = "SELL"
        elif (
            round(df_day["close_by%Boll_sum"], 2)
            == round(df_day["open_by%Boll_sum"], 2)
            == round(df_day["low_by%Boll_sum"], 2)
            == round(df_day["high_by%Boll_sum"], 2)
            and round(df_day["close_by%Boll_sum"], 2) <= 0.5
            and TradingRules.Main_compare_or(df_day, -1, num=0, mode="DOWN")
            #     df_day_1["close_by%Boll_sum"] >= 0 and df_day["close_by%Boll_sum"] < 0) or (
            #     df_day_1["close_by%Boll_sum"] == 0 and df_day["close_by%Boll_sum"] == 0
        ):
            mode = "BUY"
            tradePersent = 1

    else:  # 横盘模式
        statsdic["TYPES"] = "ROUND"
        if statsdic["Share"] > 0 and (
            round(df_day["close_by%Boll_sum"], 2)
            == round(df_day["open_by%Boll_sum"], 2)
            == round(df_day["low_by%Boll_sum"], 2)
            == round(df_day["high_by%Boll_sum"], 2)
            and round(df_day["close_by%Boll_sum"], 2) >= 0.4
            and TradingRules.Main_compare_or(df_day, 1, num=0.4, mode="UP")
            # TradingRules.Main_Plus(df, day, count_in, num=2) or
        ):
            tradePersent = 0.5
            mode = "SELL"
        elif (
            round(df_day["close_by%Boll_sum"], 2)
            == round(df_day["open_by%Boll_sum"], 2)
            == round(df_day["low_by%Boll_sum"], 2)
            == round(df_day["high_by%Boll_sum"], 2)
            and round(df_day["close_by%Boll_sum"], 2) <= -0.5
            and TradingRules.Main_compare_or(df_day, -1, num=0.4, mode="DOWN")
            #     df_day_1["close_by%Boll_sum"] >= 0 and df_day["close_by%Boll_sum"] < 0) or (
            #     df_day_1["close_by%Boll_sum"] == 0 and df_day["close_by%Boll_sum"] == 0
        ):
            mode = "BUY"
            tradePersent = 0.5

        # mode, tradePersent = RoundMode(
        #     df_d, df_day, preTYPES, mode, tradePersent, day_lists, statsdic, count_in
        # )

    return mode, tradePersent * trade_cuts